﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;

namespace BoriPeter.NutritionManager.WebFormsClient
{
    /// <summary>
    /// BasePage for the common funtionality in all 
    /// the web pages of the site.
    /// </summary>
    public class BasePage : Page
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public BasePage()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public const string LanguageDropDownName = "ctl00$cphHeader$Header1$ddlLanguage";

        public const string PostBackEventTarget = "__EVENTTARGET";

        protected override void InitializeCulture()
        {
            base.InitializeCulture();
            if (Request[PostBackEventTarget] != null)
            {
                string controlID = Request[PostBackEventTarget];
                if (!String.IsNullOrEmpty(controlID))
                {
                    string target = controlID.Split('$')[2];

                    if (target.Equals("DropDownList1"))
                    {
                        string selectedValue =
                               Request.Form[Request[PostBackEventTarget]].ToString();

                        switch (selectedValue)
                        {
                            case "hu": SetCulture("hu-HU", "hu-HU");
                                break;
                            case "en": SetCulture("en-US", "en-US");
                                break;
                            default: break;
                        }
                    }
                }
            }

            if (Session["MyUICulture"] != null && Session["MyCulture"] != null)
            {
                Thread.CurrentThread.CurrentUICulture = (CultureInfo)Session["MyUICulture"];
                Thread.CurrentThread.CurrentCulture = (CultureInfo)Session["MyCulture"];
            }
        }


        protected void SetCulture(string name, string locale)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
            ///<remarks>
            ///Saving the current thread's culture set by the User in the Session
            ///so that it can be used across the pages in the current application.
            ///</remarks>
            Session["MyUICulture"] = Thread.CurrentThread.CurrentUICulture;
            Session["MyCulture"] = Thread.CurrentThread.CurrentCulture;
        }


    }

}